//
//  HHImageMessageCell.m
//  HHChatKit
//
//  Created by Henry on 2021/6/3.
//

#import "HHImageMessageCell.h"
#import <HHTool/HHTool.h>

@implementation HHImageMessageCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _thumbView = [[UIImageView alloc] init];
        [_thumbView hh_cornerRadius:4];
        [self.container addSubview:_thumbView];
        _thumbView.mm_fill();
        _thumbView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

        _progressView = [[DACircularProgressView alloc] init];
        _progressView.roundedCorners = YES;
        [self.container addSubview:_progressView];
        
    }
    return self;
}

- (void)fillWithData:(HHImageMessageCellData *)data {
    [super fillWithData:data];
    self.imageData = data;
    
    _thumbView.image = nil;
    
    [data loadImage];

    CGFloat progress = self.imageData.thumbProgress / 100.f;
    self.progressView.hidden = (progress >= 1 || progress == 0);
    [self.progressView setProgress:progress animated:YES];

    [self.imageData addObserver:self forKeyPath:@"thumbProgress" options:NSKeyValueObservingOptionNew context:nil];
    [self.imageData addObserver:self forKeyPath:@"thumbImage" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionInitial context:nil];
    if (self.messageData.direction == HHMsgDirectionOutgoing) {
        [self.imageData addObserver:self forKeyPath:@"uploadProgress" options:NSKeyValueObservingOptionNew context:nil];
    }
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    
    if ([keyPath isEqualToString:@"thumbProgress"]) {
        CGFloat progress = self.imageData.thumbProgress / 100.f;
        NSLog(@"thumbProgress: %lf", progress);
        [self.progressView setProgress:progress animated:YES];
        self.progressView.hidden = (progress >= 1 || progress == 0);

    } else if ([keyPath isEqualToString:@"thumbImage"]) {
        self.thumbView.image = self.imageData.thumbImage;
    } else if ([keyPath isEqualToString:@"uploadProgress"]) {
        CGFloat progress = self.imageData.uploadProgress / 100.f;
        NSLog(@"uploadProgress: %lf", progress);
        [self.progressView setProgress:progress animated:YES];
        self.progressView.hidden = (progress >= 1 || progress == 0);
    }

}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.progressView.mm_center();
}

- (void)dealloc {
    if (self.imageData) {
        [self.imageData removeObserver:self forKeyPath:@"thumbProgress" context:nil];
        [self.imageData removeObserver:self forKeyPath:@"thumbImage" context:nil];
        if (self.messageData.direction == HHMsgDirectionOutgoing) {
            [self.imageData removeObserver:self forKeyPath:@"uploadProgress" context:nil];
        }
    }
}

@end
